package com.ming.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ming.domain.Prod;
import com.ming.domain.Sku;
import com.ming.service.ProdService;
import com.ming.service.SkuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author:何元才
 * @Desc:
 * @data:2021-04-01 16:51
 */
@RestController
@RequestMapping("prod/prod")
@Api(tags = "商品管理的接口")
public class ProdController {

    @Autowired
    private ProdService prodService;

    @Autowired
    private SkuService skuService;

    /**
     * 分页查询商品
     *
     * @param page
     * @param prod
     * @return
     */
    @GetMapping("page")
    @ApiOperation("分页查询商品")
    @PreAuthorize("hasAuthority('prod:prod:page')")
    public ResponseEntity<Page<Prod>> findProdPage(Page<Prod> page, Prod prod) {
        Page<Prod> prodPage = prodService.page(page, new LambdaQueryWrapper<Prod>()
                .eq(!ObjectUtils.isEmpty(prod.getStatus()), Prod::getStatus, prod.getStatus())
                .like(!StringUtils.isEmpty(prod.getProdName()), Prod::getProdName, prod.getProdName())
                .orderByDesc(Prod::getUpdateTime)
        );
        return ResponseEntity.ok(prodPage);
    }


    /**
     * 上架商品
     *
     * @param prod
     * @return
     */
    @PostMapping
    @ApiOperation("上架商品")
    @PreAuthorize("hasAuthority('prod:prod:save')")
    public ResponseEntity<Void> addProd(@RequestBody Prod prod){
        prodService.save(prod);
        return ResponseEntity.ok().build();
    }


    // -----------------------------

    @GetMapping("prod/prodInfo")
    @ApiOperation("根据商品的id查询商品的信息包含sku的集合")
    public ResponseEntity<Prod> prodInfo(Long prodId){
        Prod prod = prodService.getById(prodId);
        return ResponseEntity.ok(prod);
    }


    /**
     * 提供远程调用根据商品id查询商品对象
     *
     * @param prodId
     * @return
     */
    @PostMapping("getProdById")
    @ApiOperation("提供远程调用根据商品id查询商品对象")
    Prod getProdById(@RequestParam Long prodId) {
        return prodService.getById(prodId);
    }

    /**
     * 提供远程调用根据skuIds查询sku集合
     *
     * @param skuIds
     * @return
     */
    @PostMapping("getSkuListByIds")
    @ApiOperation("提供远程调用根据skuIds查询sku集合")
     List<Sku> getSkuListByIds(@RequestBody List<Long> skuIds) {
        return skuService.listByIds(skuIds);
    }

    /**
     * 提供远程调用修改库存
     *
     * @param map
     */
    @PostMapping("changeStock")
    @ApiOperation("提供远程调用修改库存")
    void changeStock(@RequestBody Map<String, Map<Long, Integer>> map) {
        prodService.changeStock(map);
    }


}
